const { pathToRegexp } = require("path-to-regexp")
const needTokenApi = [
    {method : "GET",path:"/api/message/addMessage"}
]



module.exports = (req,res,next)=>{
    const apis = needTokenApi.filter(api =>{
        const reg = pathToRegexp(api.path)
        return api.method === req.method && reg.test(req.path)
    })
    if(apis.length === 0){
        next()
        return;
    }
    
    let token = req.signedCookies.token;
    if(!token){
        token = req.headers.authorization;
    }
    if(!token){
        res.status(403).send("不能访问此api")
        return
    }
    next();
}